use eframe::{CreationContext, egui, Frame};
use eframe::egui::Context;
use crate::common::init_app::init_app;
use crate::db::Db;
use crate::db::users::User;

mod ui_users;
mod ui_orders;

/// 应用实体
pub struct MyApp {
    pub db: Db,
    pub path: String,
    pub users: Vec<User>,
}

impl MyApp {
    /// 初始化应用
    pub fn new(cc: &CreationContext) -> Self {
        init_app(&cc.egui_ctx);
        let db = Db::init().expect("初始化数据库失败");
        let path = "".to_string();
        Self { db, path, users: vec![] }
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            // 防止内部文字被选中
            ui.style_mut().interaction.selectable_labels = false;
            egui::TopBottomPanel::top("top_panel")
                .resizable(false).min_height(32.0)
                .show_inside(ui, |ui| {
                    ui.horizontal_wrapped(|ui| {
                        if ui.button("用户列表").clicked() {
                            self.path = "/user".to_string();
                        }
                        if ui.button("订单列表").clicked() {
                            self.path = "/order".to_string();
                        }
                    });
                });
            egui::CentralPanel::default().show_inside(ui, |ui| {
                //路由渲染
                if self.path == "" || self.path == "/user" {
                    self.ui_user_list(ui);
                }
                if self.path == "/order" {
                    self.ui_order_list(ui);
                }
            });
        });
    }
}